<?php
if ($_REQUEST['add']) {
	$extsite     = escapestr($_REQUEST['extsite']);
	$extusername = escapestr($_REQUEST['extusername']);
	$extpassword = escapestr($_REQUEST['extpassword']);
	$query = "INSERT INTO `external` (`userid`,`site`,`username`,`password`) values ('$id','$extsite','$extusername','$extpassword')";
	$result = mysql_query($query);
	if ($result) 
		$SITE_MIDDLE .= FormatElement( GetLangString ( $lang , "txt_extsite") , GetLangString ( $lang , "msg_extsiteadded")); 
	else 
		$SITE_MIDDLE .= FormatElement( GetLangString ( $lang , "txt_error") , GetLangString ( $lang , "msg_dberror"));
	
}  
if ($_REQUEST['delete']) {
	$idtodelete = escapestr($_REQUEST['delete']);
	$query = "delete from `external` where `userid`='$id' and `id`='$idtodelete'";
	$result = mysql_query($query);
	if ($result) 
		$SITE_MIDDLE .= FormatElement( GetLangString ( $lang , "txt_extsite") , GetLangString ( $lang , "msg_extsitedeleted")); 
	else 
		$SITE_MIDDLE .= FormatElement( GetLangString ( $lang , "txt_error") , GetLangString ( $lang , "msg_dberror"));	
} 
if ($_REQUEST['update']) {
	
	$extsite     = escapestr($_REQUEST['extsite']);
	$idtoupdate = escapestr($_REQUEST['update']);
	$extusername = escapestr($_REQUEST['extusername']);
	$query = "update `external` set `username`='$username', `site`='$extsite' where `id`='$idtoupdate'";
	$result = mysql_query($query);
	if (!$result)
		$SITE_MIDDLE .= FormatElement( GetLangString ( $lang , "txt_error") , GetLangString ( $lang , "msg_dberror"));	
	else {	
		if ($extsite=='last.fm' ) $table = 'lastfm';
		if ($extsite=='myanimelist' ) $table = 'mal';
		if ($table) {
			$charttype = escapestr($_REQUEST['charttype']);
			$query = "update `profileexternal` set `$table`='$charttype' where `user`='$id'"	;
			
			$result = mysql_query($query);
			if (mysql_affected_rows()==0) {
				$query = "insert into `profileexternal` (`user`,`$table`) values ('$id','$charttype')"	;
				$result = mysql_query($query);
			}
		}	
		if (!$result) {
			$SITE_MIDDLE .= FormatElement( GetLangString ( $lang , "txt_error") , GetLangString ( $lang , "msg_dberror"));	
		} else
			$SITE_MIDDLE .= FormatElement( GetLangString ( $lang , "txt_extsite") , GetLangString ( $lang , "msg_extsiteupdated"));	
		}
	
	}

if ($_REQUEST['edit']) {
$idtoupdate = escapestr($_REQUEST['edit']);
$query = "select * from `external` where `userid`='$id' and `id`='$idtoupdate'";
$result = mysql_query($query);
$extsite = mysql_fetch_array($result);


$SITE_MIDDLE .= "<form action='index.php?do=external' method='post'>
<input type=hidden name='update' value='$idtoupdate'>
	<center><table>
	<tr><td>" .  GetLangString($lang,"txt_extsite") . "</td><td>
	<input type=hidden value='".$extsite['site']."' name='extsite'> ".$extsite['site']."</td></tr>
	<tr><td>" .  GetLangString($lang,"txt_extusername") . "</td><td>
				<input value='".$extsite['username']."' type='text' name='extusername'></td></tr>	";
	
	if ($extsite['site']=="last.fm") {					 
			$SITE_MIDDLE .= "<tr><td>" .  GetLangString($lang,"txt_lastfmcharttype") . "</td><td>";
		
			$query = "select * from `profileexternal` where `user`='$id'";
			$result = mysql_query($query);
			$charttype = mysql_fetch_array($result);
			if ($charttype['lastfm']==0 ) $ch0 = 'selected'; 
			if ($charttype['lastfm']==1 ) $ch1 = 'selected'; 
			if ($charttype['lastfm']==2 ) $ch2 = 'selected'; 
			if ($charttype['lastfm']==3 ) $ch3 = 'selected'; 
			
			$SITE_MIDDLE .= "<select name=\"charttype\" size=\"1\">
			<option value=\"0\" $ch0 >".GetLangString($lang,"txt_none")."</option>
			<option value=\"1\"	$ch1 >".GetLangString($lang,"txt_lastfmtop")."</option>
			<option value=\"2\"	$ch2 >".GetLangString($lang,"txt_lastfmweekly")."</option>		
			<option value=\"3\"	$ch3 >".GetLangString($lang,"txt_lastfmrecent")."</option>		
			</select>			";
		}
			if ($extsite['site']=="myanimelist") {					 
			$SITE_MIDDLE .= "<tr><td>" .  GetLangString($lang,"txt_lastfmcharttype") . "</td><td>";
		
			$query = "select * from `profileexternal` where `user`='$id'";
			$result = mysql_query($query);
			$charttype = mysql_fetch_array($result);
			if ($charttype['mal']==0 ) $ch0 = 'selected'; 
			if ($charttype['mal']==1 ) $ch1 = 'selected'; 
			if ($charttype['mal']==2 ) $ch2 = 'selected'; 
						
			$SITE_MIDDLE .= "<select name=\"charttype\" size=\"1\">
			<option value=\"0\" $ch0 >".GetLangString($lang,"txt_none")."</option>
			<option value=\"1\"	$ch1 >".GetLangString($lang,"txt_malanime")."</option>
			<option value=\"2\"	$ch2 >".GetLangString($lang,"txt_malmanga")."</option>		
			</select>			";
		}
		
$SITE_MIDDLE .=  "	
		</td></tr>								
	</td></tr>				
	
<tr><td></td><td>
				<input type='submit' name='update' value='".GetLangString($lang,"txt_update")."'></td></tr></table></center></form>";
} else {
	$SITE_MIDDLE .= "<form action='index.php?do=external' method='post'>
	<center><table>
	<tr><td>" .  GetLangString($lang,"txt_extsite") . "</td><td>
<select name='extsite' size=\"1\">
		<option value='last.fm'  >Last.FM</option>
		<option value='myanimelist'  >MyAnimeList</option>
</select>
							
							
							</td></tr>
	<tr><td>" .  GetLangString($lang,"txt_extusername") . "</td><td>
				<input type='text' name='extusername'></td></tr>
	<!--<tr><td>" .  GetLangString($lang,"txt_lastfmcharttype") . "</td><td>
		<select name=\"charttype\" size=\"1\">
		<option value=\"0\"  >".GetLangString($lang,"txt_none")."</option>
		<option value=\"1\"	 >".GetLangString($lang,"txt_lastfmtop")."</option>
		<option value=\"2\"	 >".GetLangString($lang,"txt_lastfmweekly")."</option>		
		<option value=\"3\"	 >".GetLangString($lang,"txt_lastfmrecent")."</option>		
		</select></td></tr>-->
<tr><td></td><td>
				<input type='submit' name='add' value='".GetLangString($lang,"txt_add")."'></td></tr></table></center></form>";
}

$query = "select * from `external` where `userid`='$id'";
$result = mysql_query($query);
if ($result) {
	$SITE_MIDDLE .= "<center><table><td>". GetLangString($lang,"txt_extsite") . "</td>
	                        <td>".GetLangString($lang,"txt_extusername") . "</td>
							<td>".GetLangString($lang,"txt_type") . "</td>
							<td>".GetLangString($lang,"txt_edit") . "</td>
							<td>".GetLangString($lang,"txt_delete") . "</td></tr>";
	while ($extsite = mysql_fetch_array($result)){
		$SITE_MIDDLE .= "<tr><td>".$extsite['site']."</td>
		                     <td>".$extsite['username']."</td>
							 <td>";
		if ($extsite['site']=="last.fm") {					 
			$query2 = "select * from `profileexternal` where `user`='$id'";
			$result2 = mysql_query($query2);
			$charttype = mysql_fetch_array($result2);
			if ($charttype['lastfm']==0 ) $SITE_MIDDLE .= GetLangString($lang,"txt_none");
			if ($charttype['lastfm']==1 ) $SITE_MIDDLE .= GetLangString($lang,"txt_lastfmtop");
			if ($charttype['lastfm']==2 ) $SITE_MIDDLE .= GetLangString($lang,"txt_lastfmweekly");
			if ($charttype['lastfm']==3 ) $SITE_MIDDLE .= GetLangString($lang,"txt_lastfmrecent");
		} else
		if ($extsite['site']=="myanimelist") {					 
			$query2 = "select * from `profileexternal` where `user`='$id'";
			$result2 = mysql_query($query2);
			$charttype = mysql_fetch_array($result2);
			if ($charttype['mal']==0 ) $SITE_MIDDLE .= GetLangString($lang,"txt_none");
			if ($charttype['mal']==1 ) $SITE_MIDDLE .= GetLangString($lang,"txt_malanime");
			if ($charttype['mal']==2 ) $SITE_MIDDLE .= GetLangString($lang,"txt_malmanga");
		}

							 
		$SITE_MIDDLE .= 	 "</td>
							 <td><a href='index.php?do=external&edit=".$extsite['id']."'>".GetLangString($lang,"txt_edit")."</a></td>
							 <td><a href='index.php?do=external&delete=".$extsite['id']."'>".GetLangString($lang,"txt_delete")."</a></td>
							 </tr>";
	}
	$SITE_MIDDLE .="</table></center>";
} else
	$SITE_MIDDLE .= FormatElement( GetLangString ( $lang , "txt_error") , GetLangString ( $lang , "msg_dberror"));

?>